// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()
const db = cloud.database()
// 云函数入口函数
exports.main = async(event, context) => {
  const wxContext = cloud.getWXContext()
  const rst = await db.collection('bodydata').where({
      _openid:wxContext.OPENID
    }).orderBy('date', 'desc')
    .get();
  var bodydatas = rst.data;
  var weights = new Array();
  var BMIs = new Array();
  var dates = new Array();
  for (var i = 0; i < bodydatas.length; i++) {
    weights.unshift(bodydatas[i].weight);
    BMIs.unshift(bodydatas[i].BMI);
    var year = bodydatas[i].date.getFullYear().toString();
    var month = bodydatas[i].date.getMonth().toString();
    var date = bodydatas[i].date.getDate().toString();
    dates.unshift(year + "." + month + "." + date);
  }
  return {
    length: bodydatas.length,
    weights: weights,
    BMIs: BMIs,
    dates: dates
  }
}